package Lesson19.Notepad;

import java.util.Scanner;

public class SecuredNotepad extends SimpleNotepad implements iSecuredNotepad
{
	private String password;
	SecuredNotepad(final String title, final int pageNumbers, final String password, final String repeatPassword) 
	{
		super(title, pageNumbers);
		if (validatePassword(password, repeatPassword))
			this.password = password;
		else
		{
			System.out.println("The passwords you entered are not equal! You password is set to \"0\"    ");
			this.password = String.valueOf(0);
		}
	}
	
	private boolean validatePassword (String password, String repeatPassword)
	{
		return password.equals(repeatPassword);
	}
	
	@Override
	public boolean validatePassword()
	{
		String password;
		boolean flag = false;
		Scanner sc = new Scanner (System.in);
		for (int i=1; i<=3; i++)
		{
			System.out.println("Insert the password: ");
			password = sc.next();
			if (this.password.equals(password))
			{
				flag = true;
				break;
			}
			else
			{
				System.out.println(i!=3? "Wrong password! Please try again: (" + i + " trials remaining)" : "Wrong password! Please try again: (" + i + " trial remaining)");
			}
		}
		return flag;
	}
	
	@Override
	public void addText(int pageNumber, String newText) 
	{
		if (validatePassword())
			super.addText(pageNumber, newText);
	}
	
	@Override
	public void deleteTextInPage(Page selectedPage) 
	{
		if (validatePassword())
			super.deleteTextInPage(selectedPage);
	}
	
	@Override
	public void replaceTextInPage(int pageNumber, String newText) 
	{
		if (validatePassword())	
			super.replaceTextInPage(pageNumber, newText);
	}
	
	@Override
	public void viewPages() 
	{
		if (validatePassword())
			super.viewPages();
	}

}
